package banco;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import basicas.Locacao;
import dao.DAO;
import dao.DAOException;

public class AcoesBancoLocacao extends DAO {
	public void inserir(Locacao locacao) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            con = getConnection(true);
            pstmt = con.prepareStatement("INSERT INTO locacao (aluno_id, livro_id) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
            pstmt.setInt(1, locacao.getAluno().getId());
            pstmt.setInt(2, locacao.getLivro().getId());
            
            pstmt.executeUpdate();
            rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
            	locacao.setId(rs.getInt(1));
            }


        } catch (SQLException ex) {
            throw new DAOException(ex);
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }

                if (pstmt != null) {
                    pstmt.close();
                }

                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                throw new DAOException(ex);
            }
        }
	}
}
